/**
INVALID_PARAMETERS      = 10001     # 参数填写错误
MAYBE_WRONG_PASSWORD    = 10002     # 密码错误

TOO_MANY_WRONG_PASSWORD = 10011     # 密码错数次数过多，请稍后重试
NOT_IN_AUTH_TIME_WINDOW = 10012     # 未到允许认证时段，请稍后重试

CONFLICT_ACCOUNT_NAME   = 10021     # 账号名称冲突
CONFLICT_PRETTY_NAME    = 10022     # 账号别名冲突

DISABLED_AD_SERVER      = 10031     # AD域控已禁用
DISABLED_AD_ACCOUNT     = 10032     # AD账号已禁用
DISABLED_SYSTEM_ADMIN   = 10033     # 系统管理员已禁用
DISABLED_TENANT_ADMIN   = 10034     # 租户管理员已禁用

NOT_FOUND_CLOUD_TENANT  = 10041     # 租户不存在
NOT_FOUND_AD_ACCOUNT    = 10042     # AD账号不存在
NOT_FOUND_SYSTEM_ADMIN  = 10043     # 系统管理员不存在
NOT_FOUND_TENANT_ADMIN  = 10044     # 租户管理员不存在

UNREACHABLE_AD_SERVER   = 10051     # AD服务器连接失败
AD_ACCOUNT_NOT_IN_AD    = 10052     # 账号在AD中不存在
AUTH_FAILED_FROM_AD     = 10053     # 账号在AD中认证失败
 */

import i18n from "@/lang";
import { AuthModule } from "@/store/modules/auth";
import { Message } from 'element-ui';

export class ResponseCode {
  constructor(data: any) {
    this.result = new Promise((resolve: any, reject: any) => {
      if (data && data.code && (this as any)[data.code]) {
        (this as any)[data.code](data).then((resData: any) => {
          // console.log(resData);
          if (resData.error) {
            resolve(resData);
          } else {
            reject(resData);
          }
        });
      } else {
        this.default(data).then((resData: any) => {
          if (!resData.error) {
            resolve(resData);
          } else {
            reject(resData);
          }
        });
      }
    });
  }

  public result!: Promise<any>;

  async default(data: any) {
    if (data.code > 99 && data.code < 1000) {
      if (/^[2][0-9]*$/.test(data.code) || /^[3][0-9]*$/.test(data.code)) {
        return data.data ? data.data : data;
      } else {
        return {
          error: true,
          message: i18n.t(`actionResult.${data.code}`),
          msg: data.message,
          code: data.code
        };
      }
    }
    return data.data ? data.data : data;
  }

  async 10001(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10001")
    };
    return res;
  }

  async 10002(data: any) {
    if (data.data && data.data.chances) {
      const res = {
        error: true,
        message: i18n.t("ResCodes.10002", { chances: data.data.chances })
      };
      return res;
    } else {
      const res = {
        error: true,
        message: data.message
      };
      return res;
    }
    
  }

  /**不用400创建资源池租户验证会出错，忘记之前为什么拦截了。。。 */
  // async 400(data: any) {
  //   console.log(data);
  //   return new Error(data);
  // }

  async 10011(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10011", { wait: data.data.wait })
    };
    return res;
  }

  async 10012(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10012")
    };
    return res;
  }

  async 10021(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10021")
    };
    return res;
  }

  async 10022(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10022")
    };
    return res;
  }

  async 10031(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10031")
    };
    return res;
  }

  async 10032(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10032")
    };
    return res;
  }

  async 10033(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10033")
    };
    return res;
  }

  async 10034(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10034")
    };
    return res;
  }

  async 10041(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10041"),
      code: 10041
    };
    return res;
  }

  async 10042(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10042")
    };
    return res;
  }

  async 10043(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10043")
    };
    return res;
  }

  async 10044(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10044")
    };
    return res;
  }

  async 10051(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10051")
    };
    return res;
  }

  async 10052(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10052"),
      code: 10052
    };
    return res;
  }

  async 10053(data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.10053")
    };
    return res;
  }

  async 10054(data: any) {
    const res = {
      error: true,
      code: 10054
    };
    return res;
  }

  async 4000 (data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.4000")
    };
    return res;
  }

  async 4001 (data: any) {
    return data;
  }

  async 4010 (data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.4010")
    }
    return res;
  }

  async 401 (data: any) {
    const res = {
      error: true,
      message: i18n.t("ResCodes.4010")
    }
    if(AuthModule.authrole == "system_admin") {
      AuthModule.LogOut(true);
    } else if (AuthModule.authrole === "tenant_admin") {
      AuthModule.LogOut(false);
    }
    return res;
  }

  async 14003 (data: any) {
    if(data.code == 14003) {
      if(window.location.port == '31015') {
        window.location.href = "https://desktop-management.cmecloud.cn:8443/system/#/ram_forbidden";
      }else {
        window.location.href = "https://desktop-management.cmecloud.cn/system/#/ram_forbidden";
      }
    }
  }
}
